<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" inline :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="部门（基地）id" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="部门（基地）id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户操作描述" prop="operation">
      <el-input v-model="dataForm.operation" placeholder="用户操作描述"></el-input>
    </el-form-item>
    <el-form-item label="用户操作描述" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="用户操作描述"></el-input>
    </el-form-item>
    <el-form-item label="请求方法" prop="method">
      <el-input v-model="dataForm.method" placeholder="请求方法"></el-input>
    </el-form-item>
    <el-form-item label="请求参数" prop="params">
      <el-input v-model="dataForm.params" placeholder="请求参数"></el-input>
    </el-form-item>
    <el-form-item label="IP地址" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="IP地址"></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createId">
      <el-input v-model="dataForm.createId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="创建用户姓名" prop="createName">
      <el-input v-model="dataForm.createName" placeholder="创建用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人id" prop="updateId">
      <el-input v-model="dataForm.updateId" placeholder="修改人id"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="修改用户姓名" prop="updateName">
      <el-input v-model="dataForm.updateName" placeholder="修改用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="删除标识 -1：删除 0：正常" prop="gcFlag">
      <el-input v-model="dataForm.gcFlag" placeholder="删除标识 -1：删除 0：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deptId: '',
          username: '',
          operation: '',
          remark: '',
          method: '',
          params: '',
          ip: '',
          createId: '',
          createName: '',
          createTime: '',
          updateId: '',
          updateTime: '',
          updateName: '',
          version: '',
          gcFlag: ''
        },
        dataRule: {
          deptId: [
            { required: true, message: '部门（基地）id不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          operation: [
            { required: true, message: '用户操作描述不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '用户操作描述不能为空', trigger: 'blur' }
          ],
          method: [
            { required: true, message: '请求方法不能为空', trigger: 'blur' }
          ],
          params: [
            { required: true, message: '请求参数不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: 'IP地址不能为空', trigger: 'blur' }
          ],
          createId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createName: [
            { required: true, message: '创建用户姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          updateName: [
            { required: true, message: '修改用户姓名不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          gcFlag: [
            { required: true, message: '删除标识 -1：删除 0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussactionrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptId = data.bussactionrecord.deptId
                this.dataForm.username = data.bussactionrecord.username
                this.dataForm.operation = data.bussactionrecord.operation
                this.dataForm.remark = data.bussactionrecord.remark
                this.dataForm.method = data.bussactionrecord.method
                this.dataForm.params = data.bussactionrecord.params
                this.dataForm.ip = data.bussactionrecord.ip
                this.dataForm.createId = data.bussactionrecord.createId
                this.dataForm.createName = data.bussactionrecord.createName
                this.dataForm.createTime = data.bussactionrecord.createTime
                this.dataForm.updateId = data.bussactionrecord.updateId
                this.dataForm.updateTime = data.bussactionrecord.updateTime
                this.dataForm.updateName = data.bussactionrecord.updateName
                this.dataForm.version = data.bussactionrecord.version
                this.dataForm.gcFlag = data.bussactionrecord.gcFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussactionrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deptId': this.dataForm.deptId,
                'username': this.dataForm.username,
                'operation': this.dataForm.operation,
                'remark': this.dataForm.remark,
                'method': this.dataForm.method,
                'params': this.dataForm.params,
                'ip': this.dataForm.ip,
                'createId': this.dataForm.createId,
                'createName': this.dataForm.createName,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'updateName': this.dataForm.updateName,
                'version': this.dataForm.version,
                'gcFlag': this.dataForm.gcFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
